package com.kummuna.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.kummuna.R;
import com.kummuna.R.layout;
import com.kummuna.R.menu;
import com.kummuna.adapter.HistoryListAdapter;
import com.kummuna.config.MainConfiguration;
import com.kummuna.util.json.JSONParser;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class HistoryWaterLeak extends Activity {

	private static LayoutInflater inflater = null;
	private static String url;
	// JSON Node Names
	public static final String TAG_ID = "Id";
	public static final String TAG_USERNAME = "Username";
	public static final String TAG_STATUS = "Status";
	public static final String TAG_TIME = "Time";
	public static final String TAG_IMAGE = "Image";

	String idFromWaterLeakTabMain;
	String topicFromWaterLeakTabMain;
	String detailFromWaterLeakTabMain;
	String latitudeFromWaterLeakTabMain;
	String longtitudeFromWaterLeakTabMain;
	String distanceFromDetectShortPath;
	String dateFromDetectShortPath;
	String urltitudeFromDetectShortPath;

	public ArrayList<HashMap<String, String>> result;
	public ListView list;
	public HistoryListAdapter adapter;

	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 100;

	TextView txtlatlng;
	GoogleMap map;
	LatLng latLng;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history_water_leak);

		// get intent
		Intent itn = getIntent();
		idFromWaterLeakTabMain = itn.getStringExtra("id");
		topicFromWaterLeakTabMain = itn.getStringExtra("topic");
		detailFromWaterLeakTabMain = itn.getStringExtra("detail");
		latitudeFromWaterLeakTabMain = itn.getStringExtra("latitude");
		longtitudeFromWaterLeakTabMain = itn.getStringExtra("longtitude");
		distanceFromDetectShortPath = itn.getStringExtra("distance");
		dateFromDetectShortPath = itn.getStringExtra("date");
		urltitudeFromDetectShortPath = itn.getStringExtra("url");

		txtlatlng = (TextView) findViewById(R.id.txtlatlong);
		txtlatlng.setText(latitudeFromWaterLeakTabMain + " "
				+ longtitudeFromWaterLeakTabMain);

		// call map
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();
		map.getUiSettings().setZoomControlsEnabled(false);
		map.getUiSettings().setScrollGesturesEnabled(false);

		latLng = new LatLng(Double.parseDouble(latitudeFromWaterLeakTabMain),
				Double.parseDouble(longtitudeFromWaterLeakTabMain));

		MarkerOptions markerOptions = new MarkerOptions();
		markerOptions.position(latLng);
		markerOptions.title(latLng.latitude + " : " + latLng.longitude);

		CameraPosition cameraPosition = new CameraPosition.Builder()
				.target(latLng).zoom(15).bearing(-30).tilt(90).build();

		// map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
		map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

		// Adding marker on the GoogleMap
		map.addMarker(markerOptions);

		// call url config waterleak
		url = MainConfiguration.addShortPath;
		url = url + "?lat=" + latitudeFromWaterLeakTabMain + "&lon=" + latitudeFromWaterLeakTabMain;
		result = new ArrayList<HashMap<String, String>>();

		// Getting JSON from URL
		JSONParser jParser = new JSONParser();
		JSONObject json = jParser.getJSONFromUrl(url);
		Log.d("Return JSON : ", json.toString());
		try {
			JSONArray jArray = json.getJSONArray("Data");

			for (int i = 0; i < jArray.length(); i++) {

				HashMap<String, String> map = new HashMap<String, String>();
				JSONObject json_data = jArray.getJSONObject(i);
				map.put(TAG_ID, json_data.getString("Id"));
				map.put(TAG_USERNAME, json_data.getString("User_id"));
				map.put(TAG_STATUS, "");
				map.put(TAG_TIME, json_data.getString("Date"));
				map.put(TAG_IMAGE, json_data.getString("Image"));

				result.add(map);

			}

		}

		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

		list = (ListView) findViewById(R.id.list);
		adapter = new HistoryListAdapter(this, result);
		list.setAdapter(adapter);
		// adapter.notifyDataSetChanged();

	}

	public void nextpage(View view) {

		Intent mapIntent = new Intent(HistoryWaterLeak.this, Detectivemap.class);
		startActivity(mapIntent);
	}

}
